#!/usr/bin/env bash

set -e
source "$__SHLIB__/common.sh"

function usage() {
  cat <<EOF
Usage: mux-config [arguments] [command] [project]

Arguments:
  -h or --help          Print the usage information
  -u or --usage         Print the usage information
  -v or --verbose       Print additional information
  -n or --dryrun        Only print the command to be executed
  -y or --noconfirm     Skip confirmation

Commands:
  start                 Create project repository, branch and config, start tmux session
  delete                Delete project repository, branch and config, kill tmux session
EOF
}

function mirror_and_start() {
  local _muxproj_="$1"
  local _muxconfig_="$2"
  local _startcwd_="$3"
  if [[ -d "$GIT_WORKING_DIR_ROOT/$_muxproj_" ]]; then
    echo "Error: $GIT_WORKING_DIR_ROOT/$_muxproj_ exists!"
    if [[ $DRYRUN -ne 1 ]]; then
      return
    fi
  fi
  if [[ $DRYRUN -eq true ]]; then
    echo "sed -e \"s#\${SESSION}#$_muxproj_#\" -e \"s#\${ROOT}#$_startcwd_#\" \"$MUXDIR/project.yml.template\" > \"$_muxconfig_\""
    echo "$GIT_NEW_WORKDIR_CMD $GIT_REPO $GIT_WORKING_DIR_ROOT/$_muxproj_"
    echo "mux start \"$_muxproj_\""
  else
    sed -e "s#\${SESSION}#$_muxproj_#" -e "s#\${ROOT}#$_startcwd_#" "$MUXDIR/project.yml.template" > "$_muxconfig_"
    $GIT_NEW_WORKDIR_CMD "$GIT_REPO" "$GIT_WORKING_DIR_ROOT/$_muxproj_"
    mux start "$_muxproj_"
  fi
}

function start() {
  local _muxproj_="$1"
  local _muxconfig_="$MUXDIR/$_muxproj_.yml"
  START_CWD=${START_CWD//__MUXPROJ__/$_muxproj_}
  if [ -e "$_muxconfig_" ];
  then
    confirm "$_muxconfig_ exists, overwrite (y/n)? [n] "
    case $REPLY in
      [y]* )
        echo "$_muxconfig_ will be overwritten."
        mirror_and_start "$_muxproj_" "$_muxconfig_" "$START_CWD"
        ;;
      * )
        sed -e "s#\${SESSION}#$_muxproj_#" "$MUXDIR/project.yml.template"
        ;;
    esac
  else
    mirror_and_start "$_muxproj_" "$_muxconfig_" "$START_CWD"
  fi
}

function delete() {
  local _muxproj_="$1"
  if [ -e "$MUXDIR/$_muxproj_.yml" ];
  then
    confirm "Delete project $_muxproj_ (y/n)? [n] "
    case $REPLY in
      [y]* )
        run "mux delete $_muxproj_" "Deleting tmuxinator config $MUXDIR/$_muxproj_.yml"
        run "cd $HOME/git5repo" "Chdir to $HOME/git5repo"
        run "git branch -D $_muxproj_" "Deleting branch $_muxproj_"
        run "rm -rf $HOME/git5/$_muxproj_" "Deleting directory $HOME/git5/$_muxproj_"
        run "tmux kill-session -t $_muxproj_" "Killing tmux session $_muxproj_"
        ;;
      * )
        ;;
    esac
  else
    echo "No project named $_muxproj_ exists."
  fi
}

exit_if_not_exist tmuxinator

VERBOSE=
DRYRUN=
CONFIRM=true

if [[ -e "$HOME/.google" ]]; then
  GIT_WORKING_DIR_ROOT=$HOME/git5
  GIT_REPO=$HOME/git5repo
  GIT_NEW_WORKDIR_CMD="git5 mirror"
  START_CWD=$GIT_WORKING_DIR_ROOT/__MUXPROJ__/google3
else
  GIT_WORKING_DIR_ROOT=$HOME/Workspace
  GIT_REPO=$HOME/gitrepo
  GIT_NEW_WORKDIR_CMD="git-new-workdir"
  START_CWD=$GIT_WORKING_DIR_ROOT/__MUXPROJ__
  exit_if_not_exist $GIT_NEW_WORKDIR_CMD
fi
MUXDIR=$HOME/.tmuxinator

# NOTE: This requires GNU getopt.  On Mac OS X and FreeBSD, you have to install
# this separately. On Mac OS X, install it with "port install getopt".
TEMP=`getopt -o vnyhu --long verbose,dryrun,noconfirm,help,usage -n 'mux-config' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true; do
  case "$1" in
    -h | -u | --help | --usage ) usage; exit 0 ;;
    -v | --verbose ) VERBOSE=true; shift ;;
    -n | --dryrun ) DRYRUN=true; shift ;;
    -y | --noconfirm ) CONFIRM=true; shift ;;
    -- ) shift; break ;;
    * ) echo "Unknown option $FLAG"; usage; exit 1 ;;
  esac
done

if [[ $VERBOSE -eq true ]]; then
  echo "Verbose mode..."
  echo "Options: DRYRUN:$DRYRUN, CONFIRM:$CONFIRM, CMD:$1"
fi

case "$1" in
  start|delete )
    $1 "$2"
    ;;
  * )
    echo "Invalid command: $1" >&2
    usage
    exit 1
esac
